package com.zking.forward.index;

import com.zking.dao.UserDao;
import com.zking.entity.User;
import com.zking.util.RedirectMsg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Deregister")
public class Deregister extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        User user = (User)req.getSession().getAttribute("user");
        req.getSession().invalidate();
        boolean flag = UserDao.deleteUser(user.getEu_user_id());
        if(flag){
            req.setAttribute("msg", RedirectMsg.DeregisterSuccess);
            req.setAttribute("pageName", RedirectMsg.indexPageName);
            req.setAttribute("url", RedirectMsg.indexUrl);
            req.getRequestDispatcher("redirect.jsp").forward(req, resp);
        }
    }
}
